package com.zretc.notice.controller;


import cn.hutool.core.util.StrUtil;
import com.zretc.common.entity.Result;
import com.zretc.notice.pojo.Notice;
import com.zretc.notice.service.NoticeService;
import com.zretc.security.entity.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 功能：
 * 作者：111
 * 日期：2024/7/3 16:25
 */
@RestController
public class NoticeController {


    @Resource
    NoticeService noticeService;

    //发布公告
    @PostMapping("/notice/add")
    public Result addNotice(@RequestBody Notice notice){
        if(StrUtil.isBlank(notice.getNoticeContent())||StrUtil.isBlank(notice.getNoticeTopic())||StrUtil.isBlank(notice.getFinishTime().toString())){
            return Result.error("数据不合法");
        }
            if (!noticeService.checkNotice(notice)) {//审核
                return Result.error("公告内容包含敏感词");
            }else{
                Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
                LoginUser user = (LoginUser) authentication.getPrincipal();//获取token
                notice.setCreatedBy(user.getUser().getUsername());
                noticeService.addNotice(notice);
                return Result.success(notice);
            }
    }


    @GetMapping("/notice/shownoticedetail")
    public Result showNoticeDetail(@RequestParam Integer noticeId){
        return Result.success(noticeService.showNoticeDetail(noticeId));
    }


    //修改公告
    @PutMapping("/notice/update")
    public Result updateNotice(@RequestBody Notice notice){
        if(StrUtil.isBlank(notice.getNoticeContent())||StrUtil.isBlank(notice.getNoticeTopic())||StrUtil.isBlank(notice.getFinishTime().toString())){
            return Result.error("数据不合法");
        }
        if (!noticeService.checkNotice(notice)) {
            return Result.error("公告内容包含敏感词");
        }else{
            noticeService.updateNotice(notice);
            return Result.success(notice);
        }
    }


    //展示公告
    @GetMapping("/notice/shownotice")
    public Result showNotice(){
        return Result.success(noticeService.showNotice());
    }


    //删除公告
    @PutMapping("/notice/delete/{id}")
    public Result deleteNotice(@PathVariable("id") Integer noticeId){
            noticeService.delete(noticeId);
            return Result.success(noticeService.showNoticeDetail(noticeId));
    }

}